﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.Controls;
using YJKExcelTableUtil;
using YJKMaterialStatistic;
using YJKGeometryFuncs;
using YJKExcelFormTool;
using YJKExcelFormTool.Designer;

namespace YJKRoomBehavior
{
	internal class TableSplit
	{
		private void GetXYDpi(ref double Xdpi, ref double Ydpi)
		{
			using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
			{
				Xdpi = (double)graphics.DpiX;
				Ydpi = (double)graphics.DpiY;
			}
		}

        public TableSplit(ExternalCommandData cmdData, FlexCell.Grid grid)
		{
			this.m_cmdData = cmdData;
			this.m_uidoc = cmdData.Application.ActiveUIDocument;
			this.m_grid = grid;
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
		}

		public Result GetSplitAfterTable(ref bool bIsFinish, ref bool bIsFirst)
		{
			int num = 0;
			foreach (KeyValuePair<string, List<int>> keyValuePair in RoomPracticeForm.m_dicFloorToRowNumber)
			{
				num += keyValuePair.Value.Count;
			}
			if (this.m_nRowNumber > num + 2)
			{
				bIsFinish = true;
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			if (this.InterceptTable())
			{
				this.CreateNewGrid(ref bIsFirst);
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		public bool InterceptTable()
		{
			TableSplitJig tableSplitJig = new TableSplitJig(this.m_uidoc);
			if (tableSplitJig.DrawJigCurveRect(this.m_uidoc.ActiveView.RightDirection, "请选择表格插入点：", "请确定表格生成范围："))
			{
				double num = (double)this.m_uidoc.ActiveView.Scale / 100.0;
				XYZ xyz = new XYZ(tableSplitJig.EndPoint.X, tableSplitJig.StartPoint.Y, tableSplitJig.StartPoint.Z);
				this.m_dTableHigh = xyz.DistanceTo(tableSplitJig.EndPoint);
				this.m_dTableHigh /= num;
				if (Geometry.GreaterThan(tableSplitJig.StartPoint.Y, tableSplitJig.EndPoint.Y))
				{
					this.m_pt = tableSplitJig.StartPoint;
				}
				else
				{
					this.m_pt = tableSplitJig.EndPoint;
				}
				tableSplitJig.Dispose();
				return true;
			}
			return false;
		}

		private void CreateNewGrid(ref bool bIsFirst)
		{
			int num = 0;
			double num2 = this.UnitTransform(this.m_grid.Row(1).Height) * 100.0;
			num2 += this.UnitTransform(this.m_grid.Row(2).Height) * 100.0;
			int i = this.m_nRowNumber;
			while (i < this.m_grid.Rows)
			{
				double num3 = this.UnitTransform(this.m_grid.Row(i).Height) * 100.0;
				num2 += num3;
				if (Geometry.LessThan(this.m_dTableHigh, num2))
				{
					if (num == 0)
					{
						YJKMessageBox.Show("选择范围较小，请重新选择");
						this.m_newGrid = null;
						return;
					}
					this.CreateNewGrid(num);
					this.m_nRowNumber += num;
					return;
				}
				else
				{
					num++;
					i++;
				}
			}
			this.CreateNewGrid(this.m_grid.Rows - this.m_nRowNumber);
			bIsFirst = true;
		}

		private void CreateNewGrid(int nRow)
		{
			Dictionary<string, List<RoomData>> newGridData = this.GetNewGridData(nRow);
            this.m_newGrid = new FlexCell.Grid();
			this.m_newGrid.Cols = 14;
			this.m_newGrid.Rows = nRow + 3;
			for (int i = 0; i < this.m_newGrid.Cols; i++)
			{
				this.m_newGrid.Column(i).Width = this.m_grid.Column(i).Width;
			}
			for (int j = 1; j < 3; j++)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_newGrid.Cell(j, k).Text = this.m_grid.Cell(j, k).Text;
					if (j == 1)
					{
						if (k == 1 || k == 2 || k == 13)
						{
							this.m_newGrid.Range(j, k, j + 1, k).Merge();
						}
						else if (k == 3 || k == 5 || k == 7 || k == 9 || k == 11)
						{
							this.m_newGrid.Range(j, k, j, k + 1).Merge();
						}
					}
				}
			}
			this.SetDataToFlexCell(newGridData);
			for (int l = 0; l < this.m_newGrid.Rows; l++)
			{
				for (int m = 0; m < this.m_newGrid.Cols; m++)
				{
					Cell cell = this.m_newGrid.Cell(l, m);
					Cell cell2 = this.m_grid.Cell(l, m);
					cell.Alignment = cell2.Alignment;
					cell.Font = cell2.Font;
					cell.FontBold = cell2.FontBold;
					cell.FontItalic = cell2.FontItalic;
					cell.FontSize = cell2.FontSize;
					cell.FontUnderline = cell2.FontUnderline;
					cell.WrapText = true;
				}
				this.m_newGrid.Row(l).AutoFit();
			}
		}

		private Dictionary<string, List<RoomData>> GetNewGridData(int nRow)
		{
			List<RoomData> list = new List<RoomData>();
			for (int i = this.m_nRowNumber; i < this.m_nRowNumber + nRow; i++)
			{
				string b = string.Empty;
				foreach (KeyValuePair<string, List<int>> keyValuePair in RoomPracticeForm.m_dicFloorToRowNumber)
				{
					if (keyValuePair.Value.Contains(i))
					{
						b = keyValuePair.Key;
						break;
					}
				}
				string text = this.m_grid.Cell(i, 2).Text;
				foreach (RoomData roomData in RoomPracticeForm.m_lstRoomData)
				{
					if (roomData.RoomName == text && roomData.FloorName == b)
					{
						list.Add(roomData);
					}
				}
			}
			Dictionary<string, List<RoomData>> dictionary = new Dictionary<string, List<RoomData>>();
			foreach (RoomData roomData2 in list)
			{
				if (!dictionary.ContainsKey(roomData2.FloorName))
				{
					dictionary.Add(roomData2.FloorName, new List<RoomData>());
					dictionary[roomData2.FloorName].Add(roomData2);
				}
				else
				{
					dictionary[roomData2.FloorName].Add(roomData2);
				}
			}
			return this.Duplicate(dictionary);
		}

		private Dictionary<string, List<RoomData>> Duplicate(Dictionary<string, List<RoomData>> dicRoomData)
		{
			Dictionary<string, List<RoomData>> dictionary = new Dictionary<string, List<RoomData>>();
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicRoomData)
			{
				List<RoomData> list = new List<RoomData>();
				using (List<RoomData>.Enumerator enumerator2 = keyValuePair.Value.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						RoomData roomData = enumerator2.Current;
						if (!list.Exists((RoomData x) => x.RoomName == roomData.RoomName))
						{
							list.Add(roomData);
						}
					}
				}
				dictionary.Add(keyValuePair.Key, list);
			}
			return dictionary;
		}

		public void SetDataToFlexCell(Dictionary<string, List<RoomData>> dicNewRoomData)
		{
			int num = 3;
			int num2 = 1;
			int num3 = 0;
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicNewRoomData)
			{
				this.m_newGrid.Cell(num, num2).Text = keyValuePair.Key;
				num3 = num;
				foreach (RoomData roomData in keyValuePair.Value)
				{
					this.m_newGrid.Cell(num3, num2 + 1).Text = roomData.RoomName;
					if (this.IsFirstFloor(roomData.FloorName))
					{
						this.m_newGrid.Cell(num3, num2 + 3).Text = roomData.DatumNB;
						this.m_newGrid.Cell(num3, num2 + 2).Text = roomData.Datum;
					}
					else
					{
						this.m_newGrid.Cell(num3, num2 + 3).Text = roomData.FloorNB;
						this.m_newGrid.Cell(num3, num2 + 2).Text = roomData.Floor;
					}
					this.m_newGrid.Cell(num3, num2 + 5).Text = roomData.SkirtNB;
					this.m_newGrid.Cell(num3, num2 + 4).Text = roomData.Skirt;
					this.m_newGrid.Cell(num3, num2 + 7).Text = roomData.WallSkirtNB;
					this.m_newGrid.Cell(num3, num2 + 6).Text = roomData.WallSkirt;
					this.m_newGrid.Cell(num3, num2 + 9).Text = roomData.InnerWallNB;
					this.m_newGrid.Cell(num3, num2 + 8).Text = roomData.InnerWall;
					this.m_newGrid.Cell(num3, num2 + 11).Text = roomData.CeilingNB;
					this.m_newGrid.Cell(num3, num2 + 10).Text = roomData.Ceiling;
					this.m_newGrid.Cell(num3, num2 + 12).Text = roomData.Remarks;
					num3++;
				}
				this.m_newGrid.Range(num, num2, num3 - 1, num2).Merge();
				num = num3;
			}
		}

		private bool IsFirstFloor(string str)
		{
			string text = this.m_newGrid.Cell(3, 1).Text;
			return str == text;
		}

		private double UnitTransform(short value)
		{
			return (double)value / this.m_dpiY * 0.08333;
		}

		public ViewSelectResult CreateView(ref XYZ ptPos)
		{
			if (this.m_GridName == "")
			{
				this.m_GridName = "表格工具";
			}
			if (this.m_IfCreateNewView == CREATENEWVIEW.NONE)
			{
				if (this.m_ptPos == null)
				{
					return TableInterface.GridDrawView(this.m_cmdData, this.m_GridName);
				}
				ptPos = this.m_ptPos;
			}
			else if (this.m_IfCreateNewView == CREATENEWVIEW.NEWVIEW)
			{
				TableInterface.GridNewDrawView(this.m_cmdData, this.m_GridName);
			}
			else
			{
				if (this.m_IfCreateNewView == CREATENEWVIEW.CURRENT)
				{
					ptPos = null;
					return ViewSelectResult.VSR_Current;
				}
				return TableInterface.GridDrawView(this.m_cmdData, this.m_GridName);
			}
			return ViewSelectResult.VSR_UserSelect;
		}

		private ExternalCommandData m_cmdData;

		private UIDocument m_uidoc;

        private FlexCell.Grid m_grid;

        public FlexCell.Grid m_newGrid;

		private int m_nRowNumber = 3;

		public XYZ m_pt;

		private double m_dTableHigh;

		public GridSetCommonInfo m_gridSet = new GridSetCommonInfo();

		private double m_dpiY;

		private double m_dpiX;

		private string m_GridName = "";

		private CREATENEWVIEW m_IfCreateNewView = CREATENEWVIEW.NONE;

		private XYZ m_ptPos;
	}
}
